﻿using System;
using System.Collections.Generic;
using System.Text;
using StockFinder.IndexUrlGenerator;
using StockFinder.SymbolExtractor;
using StockFinder.Entities;

namespace StockFinder.SymbolsRetriever
{
    public class SymbolsRetrieverEngine
    {
        public List<Symbol> Run()
        {
            List<Symbol> symbols = new List<Symbol>();

            IndexUrlGeneratorEngine urlsEngine = new IndexUrlGeneratorEngine();
            List<string> urls = urlsEngine.Run();

            SymbolExtractorEngine symbolExtractorEngine = new SymbolExtractorEngine();            

            foreach (string url in urls)
            {
                //get list of symbol names from url
                List<string> names = symbolExtractorEngine.ExtractSymbolsFromUrl(url);

                //loop the names
                foreach (string symbolName in names)
                {
                    //does it already exist?
                    if (!symbols.Exists(delegate(Symbol item){ return item.Name == symbolName; }))
                    {
                        //doesnt exist so create new one
                        Symbol newSymbol = new Symbol() { Name = symbolName };

                        //add to list
                        symbols.Add(newSymbol);
                    }
                }
            }

            //we have our symbols so save them
            return symbols;
        }
    }
}
